package com.logistics.treatment.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.delivery.entity.model.DeliveryContainerSave;
import com.logistics.delivery.entity.model.DeliverySave;
import com.logistics.delivery.service.DeliveryService;
import com.logistics.inventory.entity.domain.Inventory;
import com.logistics.inventory.service.InventoryService;
import com.logistics.pick.entity.domain.PickDetail;
import com.logistics.qrcode.service.QrcodeService;
import com.logistics.treatment.entity.model.TreatmentSave;
import com.logistics.treatment.mapper.HeatTreatmentMapper;
import com.logistics.utils.constants.Constants;
import com.logistics.utils.exception.MessageException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HeatTreatmentService extends ServiceImpl<HeatTreatmentMapper, PickDetail> {

    @Autowired
    HeatTreatmentMapper heatTreatmentMapper;

    @Autowired
    QrcodeService qrcodeService;

    @Autowired
    InventoryService inventoryService;

    @Autowired
    DeliveryService deliveryService;

    public void createDelivery(TreatmentSave treatmentSave) throws MessageException {
        //站点码转码
        String point = qrcodeService.valueByCode(treatmentSave.getCode(), Constants.CODE_TYPE_LOCATION);

        //验证站点
        validate(point);

        //创建配送单
        DeliveryContainerSave deliveryContainerSave= DeliveryContainerSave.build(treatmentSave.getPick(),treatmentSave.getContainer(),1,false);
        deliveryService.save(DeliverySave.build(treatmentSave.getPick(),"0",Constants.DELIVERY_TYPE_DELIVERY,
                point,"热处理暂存区",Constants.AGV_TYPE_FORKLIFT,"1","LHGF",deliveryContainerSave));
    }

    /**
     * 验证站点
     */
    private void validate(String station) throws MessageException {
        //判断站点是否有空托盘
        List<Inventory> inventoryList = inventoryService.checkEmptyTray(station);

        if (inventoryList.isEmpty()){
            throw new MessageException("站点无可用托盘，请先呼叫空托盘");
        }
    }

}